
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>createElement · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="update.html" />
    
    
    <link rel="prev" href="virtual-dom.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../prepare/">
            
                <a href="../prepare/">
            
                    
                    准备工作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../prepare/flow.html">
            
                <a href="../prepare/flow.html">
            
                    
                    认识flow
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../prepare/directory.html">
            
                <a href="../prepare/directory.html">
            
                    
                    Vue.js源码目录设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../prepare/build.html">
            
                <a href="../prepare/build.html">
            
                    
                    Vue.js源码构建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../prepare/entrance.html">
            
                <a href="../prepare/entrance.html">
            
                    
                    从入口开始
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    数据驱动
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="./">
            
                <a href="./">
            
                    
                    数据驱动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="new-vue.html">
            
                <a href="new-vue.html">
            
                    
                    new Vue发生了什么
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="mounted.html">
            
                <a href="mounted.html">
            
                    
                    Vue实例挂载的实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="render.html">
            
                <a href="render.html">
            
                    
                    render
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="virtual-dom.html">
            
                <a href="virtual-dom.html">
            
                    
                    Visual Dom
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.6" data-path="create-element.html">
            
                <a href="create-element.html">
            
                    
                    createElement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="update.html">
            
                <a href="update.html">
            
                    
                    update
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >createElement</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="createelement">createElement</h1>
<p>Vue.js &#x5229;&#x7528; createElement &#x65B9;&#x6CD5;&#x521B;&#x5EFA; VNode&#xFF0C;&#x5B83;&#x5B9A;&#x4E49;&#x5728; <code>src/core/vdom/create-element.js</code> &#x4E2D;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// wrapper function for providing a more flexible interface</span>
<span class="hljs-comment">// without getting yelled at by flow</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createElement</span> (<span class="hljs-params">
  context: Component,
  tag: any,
  data: any,
  children: any,
  normalizationType: any,
  alwaysNormalize: boolean
</span>): <span class="hljs-title">VNode</span> | <span class="hljs-title">Array</span>&lt;<span class="hljs-title">VNode</span>&gt; </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(data) || isPrimitive(data)) {
    normalizationType = children
    children = data
    data = <span class="hljs-literal">undefined</span>
  }
  <span class="hljs-keyword">if</span> (isTrue(alwaysNormalize)) {
    normalizationType = ALWAYS_NORMALIZE
  }
  <span class="hljs-keyword">return</span> _createElement(context, tag, data, children, normalizationType)
}
</code></pre>
<p><code>createElement</code> &#x65B9;&#x6CD5;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x5BF9; <code>_createElement</code> &#x65B9;&#x6CD5;&#x7684;&#x5C01;&#x88C5;&#xFF0C;&#x5B83;&#x5141;&#x8BB8;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x66F4;&#x52A0;&#x7075;&#x6D3B;&#xFF0C;&#x5728;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x53C2;&#x6570;&#x540E;&#xFF0C;&#x8C03;&#x7528;&#x771F;&#x6B63;&#x521B;&#x5EFA; VNode &#x7684;&#x51FD;&#x6570; <code>_createElement</code>&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_createElement</span> (<span class="hljs-params">
  context: Component,
  tag?: string | Class&lt;Component&gt; | Function | Object,
  data?: VNodeData,
  children?: any,
  normalizationType?: number
</span>): <span class="hljs-title">VNode</span> | <span class="hljs-title">Array</span>&lt;<span class="hljs-title">VNode</span>&gt; </span>{
  <span class="hljs-keyword">if</span> (isDef(data) &amp;&amp; isDef((data: any).__ob__)) {
    process.env.NODE_ENV !== <span class="hljs-string">&apos;production&apos;</span> &amp;&amp; warn(
      <span class="hljs-string">`Avoid using observed data object as vnode data: <span class="hljs-subst">${JSON.stringify(data)}</span>\n`</span> +
      <span class="hljs-string">&apos;Always create fresh vnode data objects in each render!&apos;</span>,
      context
    )
    <span class="hljs-keyword">return</span> createEmptyVNode()
  }
  <span class="hljs-comment">// object syntax in v-bind</span>
  <span class="hljs-keyword">if</span> (isDef(data) &amp;&amp; isDef(data.is)) {
    tag = data.is
  }
  <span class="hljs-keyword">if</span> (!tag) {
    <span class="hljs-comment">// in case of component :is set to falsy value</span>
    <span class="hljs-keyword">return</span> createEmptyVNode()
  }
  <span class="hljs-comment">// warn against non-primitive key</span>
  <span class="hljs-keyword">if</span> (process.env.NODE_ENV !== <span class="hljs-string">&apos;production&apos;</span> &amp;&amp;
    isDef(data) &amp;&amp; isDef(data.key) &amp;&amp; !isPrimitive(data.key)
  ) {
    <span class="hljs-keyword">if</span> (!__WEEX__ || !(<span class="hljs-string">&apos;@binding&apos;</span> <span class="hljs-keyword">in</span> data.key)) {
      warn(
        <span class="hljs-string">&apos;Avoid using non-primitive value as key, &apos;</span> +
        <span class="hljs-string">&apos;use string/number value instead.&apos;</span>,
        context
      )
    }
  }
  <span class="hljs-comment">// support single function children as default scoped slot</span>
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(children) &amp;&amp;
    <span class="hljs-keyword">typeof</span> children[<span class="hljs-number">0</span>] === <span class="hljs-string">&apos;function&apos;</span>
  ) {
    data = data || {}
    data.scopedSlots = { <span class="hljs-keyword">default</span>: children[<span class="hljs-number">0</span>] }
    children.length = <span class="hljs-number">0</span>
  }
  <span class="hljs-keyword">if</span> (normalizationType === ALWAYS_NORMALIZE) {
    children = normalizeChildren(children)
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (normalizationType === SIMPLE_NORMALIZE) {
    children = simpleNormalizeChildren(children)
  }
  <span class="hljs-keyword">let</span> vnode, ns
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> tag === <span class="hljs-string">&apos;string&apos;</span>) {
    <span class="hljs-keyword">let</span> Ctor
    ns = (context.$vnode &amp;&amp; context.$vnode.ns) || config.getTagNamespace(tag)
    <span class="hljs-keyword">if</span> (config.isReservedTag(tag)) {
      <span class="hljs-comment">// platform built-in elements</span>
      vnode = <span class="hljs-keyword">new</span> VNode(
        config.parsePlatformTagName(tag), data, children,
        <span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>, context
      )
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isDef(Ctor = resolveAsset(context.$options, <span class="hljs-string">&apos;components&apos;</span>, tag))) {
      <span class="hljs-comment">// component</span>
      vnode = createComponent(Ctor, data, context, children, tag)
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// unknown or unlisted namespaced elements</span>
      <span class="hljs-comment">// check at runtime because it may get assigned a namespace when its</span>
      <span class="hljs-comment">// parent normalizes children</span>
      vnode = <span class="hljs-keyword">new</span> VNode(
        tag, data, children,
        <span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>, context
      )
    }
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// direct component options / constructor</span>
    vnode = createComponent(tag, data, context, children)
  }
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(vnode)) {
    <span class="hljs-keyword">return</span> vnode
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isDef(vnode)) {
    <span class="hljs-keyword">if</span> (isDef(ns)) applyNS(vnode, ns)
    <span class="hljs-keyword">if</span> (isDef(data)) registerDeepBindings(data)
    <span class="hljs-keyword">return</span> vnode
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> createEmptyVNode()
  }
}
</code></pre>
<p><code>_createElement</code> &#x65B9;&#x6CD5;&#x6709; 5 &#x4E2A;&#x53C2;&#x6570;&#xFF0C;<code>context</code> &#x8868;&#x793A; VNode &#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;&#xFF0C;&#x5B83;&#x662F; <code>Component</code> &#x7C7B;&#x578B;&#xFF1B;<code>tag</code> &#x8868;&#x793A;&#x6807;&#x7B7E;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A; <code>Component</code>&#xFF1B;<code>data</code> &#x8868;&#x793A; VNode &#x7684;&#x6570;&#x636E;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A; <code>VNodeData</code> &#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x5728; <code>flow/vnode.js</code> &#x4E2D;&#x627E;&#x5230;&#x5B83;&#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x8FD9;&#x91CC;&#x5148;&#x4E0D;&#x5C55;&#x5F00;&#x8BF4;&#xFF1B;<code>children</code> &#x8868;&#x793A;&#x5F53;&#x524D; VNode &#x7684;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x5B83;&#x662F;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#xFF0C;&#x5B83;&#x63A5;&#x4E0B;&#x6765;&#x9700;&#x8981;&#x88AB;&#x89C4;&#x8303;&#x4E3A;&#x6807;&#x51C6;&#x7684; VNode &#x6570;&#x7EC4;&#xFF1B;<code>normalizationType</code> &#x8868;&#x793A;&#x5B50;&#x8282;&#x70B9;&#x89C4;&#x8303;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x7C7B;&#x578B;&#x4E0D;&#x540C;&#x89C4;&#x8303;&#x7684;&#x65B9;&#x6CD5;&#x4E5F;&#x5C31;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x5B83;&#x4E3B;&#x8981;&#x662F;&#x53C2;&#x8003; <code>render</code> &#x51FD;&#x6570;&#x662F;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x8FD8;&#x662F;&#x7528;&#x6237;&#x624B;&#x5199;&#x7684;&#x3002;</p>
<p><code>createElement</code> &#x51FD;&#x6570;&#x7684;&#x6D41;&#x7A0B;&#x7565;&#x5FAE;&#x6709;&#x70B9;&#x591A;&#xFF0C;&#x6211;&#x4EEC;&#x63A5;&#x4E0B;&#x6765;&#x4E3B;&#x8981;&#x5206;&#x6790; 2 &#x4E2A;&#x91CD;&#x70B9;&#x7684;&#x6D41;&#x7A0B; &#x2014;&#x2014; <code>children</code> &#x7684;&#x89C4;&#x8303;&#x5316;&#x4EE5;&#x53CA; VNode &#x7684;&#x521B;&#x5EFA;&#x3002;</p>
<h2 id="children-&#x7684;&#x89C4;&#x8303;&#x5316;">children &#x7684;&#x89C4;&#x8303;&#x5316;</h2>
<p>&#x7531;&#x4E8E; Virtual DOM &#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x6811;&#x72B6;&#x7ED3;&#x6784;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A; VNode &#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x82E5;&#x5E72;&#x4E2A;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x8FD9;&#x4E9B;&#x5B50;&#x8282;&#x70B9;&#x5E94;&#x8BE5;&#x4E5F;&#x662F; VNode &#x7684;&#x7C7B;&#x578B;&#x3002;<code>_createElement</code> &#x63A5;&#x6536;&#x7684;&#x7B2C; 4 &#x4E2A;&#x53C2;&#x6570; children &#x662F;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x628A;&#x5B83;&#x4EEC;&#x89C4;&#x8303;&#x6210; VNode &#x7C7B;&#x578B;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x6839;&#x636E; <code>normalizationType</code> &#x7684;&#x4E0D;&#x540C;&#xFF0C;&#x8C03;&#x7528;&#x4E86; <code>normalizeChildren(children)</code> &#x548C; <code>simpleNormalizeChildren(children)</code> &#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x5B9A;&#x4E49;&#x90FD;&#x5728; <code>src/core/vdom/helpers/normalzie-children.js</code> &#x4E2D;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// The template compiler attempts to minimize the need for normalization by</span>
<span class="hljs-comment">// statically analyzing the template at compile time.</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// For plain HTML markup, normalization can be completely skipped because the</span>
<span class="hljs-comment">// generated render function is guaranteed to return Array&lt;VNode&gt;. There are</span>
<span class="hljs-comment">// two cases where extra normalization is needed:</span>

<span class="hljs-comment">// 1. When the children contains components - because a functional component</span>
<span class="hljs-comment">// may return an Array instead of a single root. In this case, just a simple</span>
<span class="hljs-comment">// normalization is needed - if any child is an Array, we flatten the whole</span>
<span class="hljs-comment">// thing with Array.prototype.concat. It is guaranteed to be only 1-level deep</span>
<span class="hljs-comment">// because functional components already normalize their own children.</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">simpleNormalizeChildren</span> (<span class="hljs-params">children: any</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; children.length; i++) {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(children[i])) {
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>.prototype.concat.apply([], children)
    }
  }
  <span class="hljs-keyword">return</span> children
}

<span class="hljs-comment">// 2. When the children contains constructs that always generated nested Arrays,</span>
<span class="hljs-comment">// e.g. &lt;template&gt;, &lt;slot&gt;, v-for, or when the children is provided by user</span>
<span class="hljs-comment">// with hand-written render functions / JSX. In such cases a full normalization</span>
<span class="hljs-comment">// is needed to cater to all possible types of children values.</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">normalizeChildren</span> (<span class="hljs-params">children: any</span>): ?<span class="hljs-title">Array</span>&lt;<span class="hljs-title">VNode</span>&gt; </span>{
  <span class="hljs-keyword">return</span> isPrimitive(children)
    ? [createTextVNode(children)]
    : <span class="hljs-built_in">Array</span>.isArray(children)
      ? normalizeArrayChildren(children)
      : <span class="hljs-literal">undefined</span>
}
</code></pre>
<p><code>simpleNormalizeChildren</code> &#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x573A;&#x666F;&#x662F; <code>render</code> &#x51FD;&#x6570;&#x662F;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x3002;&#x7406;&#x8BBA;&#x4E0A;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684; <code>children</code> &#x90FD;&#x5DF2;&#x7ECF;&#x662F; VNode &#x7C7B;&#x578B;&#x7684;&#xFF0C;&#x4F46;&#x8FD9;&#x91CC;&#x6709;&#x4E00;&#x4E2A;&#x4F8B;&#x5916;&#xFF0C;&#x5C31;&#x662F; <code>functional component</code> &#x51FD;&#x6570;&#x5F0F;&#x7EC4;&#x4EF6;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x6839;&#x8282;&#x70B9;&#xFF0C;&#x6240;&#x4EE5;&#x4F1A;&#x901A;&#x8FC7; <code>Array.prototype.concat</code> &#x65B9;&#x6CD5;&#x628A;&#x6574;&#x4E2A; <code>children</code> &#x6570;&#x7EC4;&#x6253;&#x5E73;&#xFF0C;&#x8BA9;&#x5B83;&#x7684;&#x6DF1;&#x5EA6;&#x53EA;&#x6709;&#x4E00;&#x5C42;&#x3002;</p>
<p><code>normalizeChildren</code> &#x65B9;&#x6CD5;&#x7684;&#x8C03;&#x7528;&#x573A;&#x666F;&#x6709; 2 &#x79CD;&#xFF0C;&#x4E00;&#x4E2A;&#x573A;&#x666F;&#x662F; <code>render</code> &#x51FD;&#x6570;&#x662F;&#x7528;&#x6237;&#x624B;&#x5199;&#x7684;&#xFF0C;&#x5F53; <code>children</code> &#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x7684;&#x65F6;&#x5019;&#xFF0C;Vue.js &#x4ECE;&#x63A5;&#x53E3;&#x5C42;&#x9762;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x628A; <code>children</code> &#x5199;&#x6210;&#x57FA;&#x7840;&#x7C7B;&#x578B;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x5355;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6587;&#x672C;&#x8282;&#x70B9;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4F1A;&#x8C03;&#x7528; <code>createTextVNode</code> &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6587;&#x672C;&#x8282;&#x70B9;&#x7684; VNode&#xFF1B;&#x53E6;&#x4E00;&#x4E2A;&#x573A;&#x666F;&#x662F;&#x5F53;&#x7F16;&#x8BD1; <code>slot</code>&#x3001;<code>v-for</code> &#x7684;&#x65F6;&#x5019;&#x4F1A;&#x4EA7;&#x751F;&#x5D4C;&#x5957;&#x6570;&#x7EC4;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x4F1A;&#x8C03;&#x7528; <code>normalizeArrayChildren</code> &#x65B9;&#x6CD5;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x5B83;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">normalizeArrayChildren</span> (<span class="hljs-params">children: any, nestedIndex?: string</span>): <span class="hljs-title">Array</span>&lt;<span class="hljs-title">VNode</span>&gt; </span>{
  <span class="hljs-keyword">const</span> res = []
  <span class="hljs-keyword">let</span> i, c, lastIndex, last
  <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; children.length; i++) {
    c = children[i]
    <span class="hljs-keyword">if</span> (isUndef(c) || <span class="hljs-keyword">typeof</span> c === <span class="hljs-string">&apos;boolean&apos;</span>) <span class="hljs-keyword">continue</span>
    lastIndex = res.length - <span class="hljs-number">1</span>
    last = res[lastIndex]
    <span class="hljs-comment">//  nested</span>
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(c)) {
      <span class="hljs-keyword">if</span> (c.length &gt; <span class="hljs-number">0</span>) {
        c = normalizeArrayChildren(c, <span class="hljs-string">`<span class="hljs-subst">${nestedIndex || &apos;&apos;}</span>_<span class="hljs-subst">${i}</span>`</span>)
        <span class="hljs-comment">// merge adjacent text nodes</span>
        <span class="hljs-keyword">if</span> (isTextNode(c[<span class="hljs-number">0</span>]) &amp;&amp; isTextNode(last)) {
          res[lastIndex] = createTextVNode(last.text + (c[<span class="hljs-number">0</span>]: any).text)
          c.shift()
        }
        res.push.apply(res, c)
      }
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isPrimitive(c)) {
      <span class="hljs-keyword">if</span> (isTextNode(last)) {
        <span class="hljs-comment">// merge adjacent text nodes</span>
        <span class="hljs-comment">// this is necessary for SSR hydration because text nodes are</span>
        <span class="hljs-comment">// essentially merged when rendered to HTML strings</span>
        res[lastIndex] = createTextVNode(last.text + c)
      } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (c !== <span class="hljs-string">&apos;&apos;</span>) {
        <span class="hljs-comment">// convert primitive to vnode</span>
        res.push(createTextVNode(c))
      }
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-keyword">if</span> (isTextNode(c) &amp;&amp; isTextNode(last)) {
        <span class="hljs-comment">// merge adjacent text nodes</span>
        res[lastIndex] = createTextVNode(last.text + c.text)
      } <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// default key for nested array children (likely generated by v-for)</span>
        <span class="hljs-keyword">if</span> (isTrue(children._isVList) &amp;&amp;
          isDef(c.tag) &amp;&amp;
          isUndef(c.key) &amp;&amp;
          isDef(nestedIndex)) {
          c.key = <span class="hljs-string">`__vlist<span class="hljs-subst">${nestedIndex}</span>_<span class="hljs-subst">${i}</span>__`</span>
        }
        res.push(c)
      }
    }
  }
  <span class="hljs-keyword">return</span> res
}
</code></pre>
<p><code>normalizeArrayChildren</code> &#x63A5;&#x6536; 2 &#x4E2A;&#x53C2;&#x6570;&#xFF0C;<code>children</code> &#x8868;&#x793A;&#x8981;&#x89C4;&#x8303;&#x7684;&#x5B50;&#x8282;&#x70B9;&#xFF0C;<code>nestedIndex</code> &#x8868;&#x793A;&#x5D4C;&#x5957;&#x7684;&#x7D22;&#x5F15;&#xFF0C;&#x56E0;&#x4E3A;&#x5355;&#x4E2A; <code>child</code> &#x53EF;&#x80FD;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x7C7B;&#x578B;&#x3002; <code>normalizeArrayChildren</code> &#x4E3B;&#x8981;&#x7684;&#x903B;&#x8F91;&#x5C31;&#x662F;&#x904D;&#x5386; <code>children</code>&#xFF0C;&#x83B7;&#x5F97;&#x5355;&#x4E2A;&#x8282;&#x70B9; <code>c</code>&#xFF0C;&#x7136;&#x540E;&#x5BF9; <code>c</code> &#x7684;&#x7C7B;&#x578B;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x9012;&#x5F52;&#x8C03;&#x7528; <code>normalizeArrayChildren</code>; &#x5982;&#x679C;&#x662F;&#x57FA;&#x7840;&#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x901A;&#x8FC7; <code>createTextVNode</code> &#x65B9;&#x6CD5;&#x8F6C;&#x6362;&#x6210; VNode &#x7C7B;&#x578B;&#xFF1B;&#x5426;&#x5219;&#x5C31;&#x5DF2;&#x7ECF;&#x662F; VNode &#x7C7B;&#x578B;&#x4E86;&#xFF0C;&#x5982;&#x679C; <code>children</code> &#x662F;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x5E76;&#x4E14;&#x5217;&#x8868;&#x8FD8;&#x5B58;&#x5728;&#x5D4C;&#x5957;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x5219;&#x6839;&#x636E; <code>nestedIndex</code> &#x53BB;&#x66F4;&#x65B0;&#x5B83;&#x7684; key&#x3002;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4E00;&#x70B9;&#xFF0C;&#x5728;&#x904D;&#x5386;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5BF9;&#x8FD9; 3 &#x79CD;&#x60C5;&#x51B5;&#x90FD;&#x505A;&#x4E86;&#x5982;&#x4E0B;&#x5904;&#x7406;&#xFF1A;&#x5982;&#x679C;&#x5B58;&#x5728;&#x4E24;&#x4E2A;&#x8FDE;&#x7EED;&#x7684; <code>text</code> &#x8282;&#x70B9;&#xFF0C;&#x4F1A;&#x628A;&#x5B83;&#x4EEC;&#x5408;&#x5E76;&#x6210;&#x4E00;&#x4E2A; <code>text</code> &#x8282;&#x70B9;&#x3002;</p>
<p>&#x7ECF;&#x8FC7;&#x5BF9; <code>children</code> &#x7684;&#x89C4;&#x8303;&#x5316;&#xFF0C;<code>children</code> &#x53D8;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x4E3A; VNode &#x7684; Array&#x3002;</p>
<h2 id="vnode-&#x7684;&#x521B;&#x5EFA;">VNode &#x7684;&#x521B;&#x5EFA;</h2>
<p>&#x56DE;&#x5230; <code>createElement</code> &#x51FD;&#x6570;&#xFF0C;&#x89C4;&#x8303;&#x5316; <code>children</code> &#x540E;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x4F1A;&#x53BB;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; VNode &#x7684;&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> vnode, ns
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> tag === <span class="hljs-string">&apos;string&apos;</span>) {
  <span class="hljs-keyword">let</span> Ctor
  ns = (context.$vnode &amp;&amp; context.$vnode.ns) || config.getTagNamespace(tag)
  <span class="hljs-keyword">if</span> (config.isReservedTag(tag)) {
    <span class="hljs-comment">// platform built-in elements</span>
    vnode = <span class="hljs-keyword">new</span> VNode(
      config.parsePlatformTagName(tag), data, children,
      <span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>, context
    )
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isDef(Ctor = resolveAsset(context.$options, <span class="hljs-string">&apos;components&apos;</span>, tag))) {
    <span class="hljs-comment">// component</span>
    vnode = createComponent(Ctor, data, context, children, tag)
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// unknown or unlisted namespaced elements</span>
    <span class="hljs-comment">// check at runtime because it may get assigned a namespace when its</span>
    <span class="hljs-comment">// parent normalizes children</span>
    vnode = <span class="hljs-keyword">new</span> VNode(
      tag, data, children,
      <span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>, context
    )
  }
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">// direct component options / constructor</span>
  vnode = createComponent(tag, data, context, children)
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x5148;&#x5BF9; <code>tag</code> &#x505A;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x662F; <code>string</code> &#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x63A5;&#x7740;&#x5224;&#x65AD;&#x5982;&#x679C;&#x662F;&#x5185;&#x7F6E;&#x7684;&#x4E00;&#x4E9B;&#x8282;&#x70B9;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x666E;&#x901A; VNode&#xFF0C;&#x5982;&#x679C;&#x662F;&#x4E3A;&#x5DF2;&#x6CE8;&#x518C;&#x7684;&#x7EC4;&#x4EF6;&#x540D;&#xFF0C;&#x5219;&#x901A;&#x8FC7; <code>createComponent</code> &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x7C7B;&#x578B;&#x7684; VNode&#xFF0C;&#x5426;&#x5219;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x672A;&#x77E5;&#x7684;&#x6807;&#x7B7E;&#x7684; VNode&#x3002; &#x5982;&#x679C;&#x662F; <code>tag</code> &#x4E00;&#x4E2A; <code>Component</code> &#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x8C03;&#x7528; <code>createComponent</code> &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x7C7B;&#x578B;&#x7684; VNode &#x8282;&#x70B9;&#x3002;&#x5BF9;&#x4E8E; <code>createComponent</code> &#x521B;&#x5EFA;&#x7EC4;&#x4EF6;&#x7C7B;&#x578B;&#x7684; VNode &#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x4E4B;&#x540E;&#x4F1A;&#x53BB;&#x4ECB;&#x7ECD;&#xFF0C;&#x672C;&#x8D28;&#x4E0A;&#x5B83;&#x8FD8;&#x662F;&#x8FD4;&#x56DE;&#x4E86;&#x4E00;&#x4E2A; VNode&#x3002;</p>
<h2 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h2>
<p>&#x90A3;&#x4E48;&#x81F3;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5927;&#x81F4;&#x4E86;&#x89E3;&#x4E86; <code>createElement</code> &#x521B;&#x5EFA; VNode &#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x6BCF;&#x4E2A; VNode &#x6709; <code>children</code>&#xFF0C;<code>children</code> &#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x4E5F;&#x662F;&#x4E00;&#x4E2A; VNode&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x5F62;&#x6210;&#x4E86;&#x4E00;&#x4E2A; VNode Tree&#xFF0C;&#x5B83;&#x5F88;&#x597D;&#x7684;&#x63CF;&#x8FF0;&#x4E86;&#x6211;&#x4EEC;&#x7684; DOM Tree&#x3002;</p>
<p>&#x56DE;&#x5230; <code>mountComponent</code> &#x51FD;&#x6570;&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x77E5;&#x9053; <code>vm._render</code> &#x662F;&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A; VNode&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x5C31;&#x662F;&#x8981;&#x628A;&#x8FD9;&#x4E2A; VNode &#x6E32;&#x67D3;&#x6210;&#x4E00;&#x4E2A;&#x771F;&#x5B9E;&#x7684; DOM &#x5E76;&#x6E32;&#x67D3;&#x51FA;&#x6765;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x662F;&#x901A;&#x8FC7; <code>vm._update</code> &#x5B8C;&#x6210;&#x7684;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x5206;&#x6790;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="virtual-dom.html" class="navigation navigation-prev " aria-label="Previous page: Visual Dom">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="update.html" class="navigation navigation-next " aria-label="Next page: update">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"createElement","level":"1.3.6","depth":2,"next":{"title":"update","level":"1.3.7","depth":2,"path":"v2/data-driven/update.md","ref":"v2/data-driven/update.md","articles":[]},"previous":{"title":"Visual Dom","level":"1.3.5","depth":2,"path":"v2/data-driven/virtual-dom.md","ref":"v2/data-driven/virtual-dom.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"v2/data-driven/create-element.md","mtime":"2021-02-02T00:48:53.869Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-02-02T01:41:31.061Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

